package com.hmy.finance.common.properties;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.validation.annotation.Validated;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 应收盘点逾期规则配置
 *
 * @author songkf
 */
@Data
@Validated
@Configuration
@ConfigurationProperties(prefix = ReceivableInventoryOverdueRule.PREFIX)
public class ReceivableInventoryOverdueRule {
    public static final String PREFIX = "hmy.finance.receivable-inventory";

    @Valid
    @NotNull
    private List<Rule> overdueRules;

    public Rule getRuleByDays(Integer overdueDaysStart, Integer overdueDaysEnd) {
        return overdueRules.stream()
                .filter(rule -> rule.getStartDays() <= overdueDaysStart && rule.getEndDays() >= overdueDaysEnd)
                .findFirst()
                .orElse(null);
    }

    public Rule getRule(String periodKey) {
        return overdueRules.stream()
                .filter(rule -> rule.getRuleKey().equals(periodKey))
                .findFirst()
                .orElse(null);
    }

    /**
     * 逾期规则
     */
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class Rule {
        @NotBlank
        private String ruleName;
        private String ruleKey;
        private int ruleSort;
        /**
         * 逾期起始天数
         */
        private Integer startDays;
        /**
         * 逾期结束天数
         */
        private Integer endDays;
    }
}
